home *** CD-ROM | disk | FTP | other *** search
/ Freelog 15 / FREELOG 15.ISO / WebMaster / Perl / PERL5106.ZIP / perl5 / Lib / perl5db.pl < prev    next >
Encoding:
Perl Script  |  1995-12-02  |  15.4 KB  |  575 lines

  1. package DB;
  2.  
  3. # modified Perl debugger, to be run from Emacs in perldb-mode
  4. # Ray Lischner (uunet!mntgfx!lisch) as of 5 Nov 1990
  5. # Johan Vromans -- upgrade to 4.0 pl 10
  6.  
  7. $header = '$RCSfile: perl5db.pl,v $$Revision: 4.1 $$Date: 92/08/07 18:24:07 $';
  8. #
  9. # This file is automatically included if you do perl -d.
  10. # It's probably not useful to include this yourself.
  11. #
  12. # Perl supplies the values for @line and %sub.  It effectively inserts
  13. # a &DB'DB(<linenum>); in front of every place that can
  14. # have a breakpoint.  It also inserts a do 'perldb.pl' before the first line.
  15. #
  16. # $Log:    perldb.pl,v $
  17.  
  18. require Config;
  19.  
  20. local($^W) = 0;
  21.  
  22. if (-e "/dev/tty") {
  23.     $console = "/dev/tty";
  24.     $rcfile=".perldb";
  25. }
  26. elsif (-e "con" || ( $Config::Config{osname} eq 'MSWin32' ) ) {
  27.     $console = "con";
  28.     $rcfile="perldb.ini";
  29. else {
  30.     $console = "sys\$command";
  31.     $rcfile="perldb.ini";
  32. }
  33.  
  34. open(IN, "<$console") || open(IN,  "<&STDIN");    # so we don't dingle stdin
  35. open(OUT,">$console") || open(OUT, ">&STDERR")
  36.     || open(OUT, ">&STDOUT");    # so we don't dongle stdout
  37. select(OUT);
  38. $| = 1;                # for DB::OUT
  39. select(STDOUT);
  40. $| = 1;                # for real STDOUT
  41. $sub = '';
  42.  
  43. # Is Perl being run from Emacs?
  44. $emacs = $main::ARGV[0] eq '-emacs';
  45. shift(@main::ARGV) if $emacs;
  46.  
  47. $header =~ s/.Header: ([^,]+),v(\s+\S+\s+\S+).*$/$1$2/;
  48. print OUT "\nLoading DB routines from $header\n";
  49. print OUT ("Emacs support ",
  50.        $emacs ? "enabled" : "available",
  51.        ".\n");
  52. print OUT "\nEnter h for help.\n\n";
  53.  
  54. @ARGS;
  55.  
  56. sub DB {
  57.     &save;
  58.     ($pkg, $filename, $line) = caller;
  59.     $usercontext = '($@, $!, $,, $/, $\, $^W) = @saved;' .
  60.     "package $pkg;";        # this won't let them modify, alas
  61.     local(*dbline) = "::_<$filename";
  62.     $max = $#dbline;
  63.     if (($stop,$action) = split(/\0/,$dbline{$line})) {
  64.     if ($stop eq '1') {
  65.         $signal |= 1;
  66.     }
  67.     else {
  68.         $evalarg = "\$DB::signal |= do {$stop;}"; &eval;
  69.         $dbline{$line} =~ s/;9($|\0)/$1/;
  70.     }
  71.     }
  72.     if ($single || $trace || $signal) {
  73.     if ($emacs) {
  74.         print OUT "\032\032$filename:$line:0\n";
  75.     } else {
  76.         $prefix = $sub =~ /'|::/ ? "" : "${pkg}::";
  77.         $prefix .= "$sub($filename:";
  78.         if (length($prefix) > 30) {
  79.         print OUT "$prefix$line):\n$line:\t",$dbline[$line];
  80.         $prefix = "";
  81.         $infix = ":\t";
  82.         }
  83.         else {
  84.         $infix = "):\t";
  85.         print OUT "$prefix$line$infix",$dbline[$line];
  86.         }
  87.         for ($i = $line + 1; $i <= $max && $dbline[$i] == 0; ++$i) {
  88.         last if $dbline[$i] =~ /^\s*(}|#|\n)/;
  89.         print OUT "$prefix$i$infix",$dbline[$i];
  90.         }
  91.     }
  92.     }
  93.     $evalarg = $action, &eval if $action;
  94.     if ($single || $signal) {
  95.     $evalarg = $pre, &eval if $pre;
  96.     print OUT $#stack . " levels deep in subroutine calls!\n"
  97.         if $single & 4;
  98.     $start = $line;
  99.       CMD:
  100.     while ((print OUT "  DB<", $#hist+1, "> "), $cmd=&gets) {
  101.         {
  102.         $single = 0;
  103.         $signal = 0;
  104.         $cmd eq '' && exit 0;
  105.         chop($cmd);
  106.         $cmd =~ s/\\$// && do {
  107.             print OUT "  cont: ";
  108.             $cmd .= &gets;
  109.             redo CMD;
  110.         };
  111.         $cmd =~ /^q$/ && exit 0;
  112.         $cmd =~ /^$/ && ($cmd = $laststep);
  113.         push(@hist,$cmd) if length($cmd) > 1;
  114.         ($i) = split(/\s+/,$cmd);
  115.         eval "\$cmd =~ $alias{$i}", print OUT $@ if $alias{$i};
  116.         $cmd =~ /^h$/ && do {
  117.             print OUT "
  118. T        Stack trace.
  119. s        Single step.
  120. n        Next, steps over subroutine calls.
  121. r        Return from current subroutine.
  122. c [line]    Continue; optionally inserts a one-time-only breakpoint 
  123.         at the specified line.
  124. <CR>        Repeat last n or s.
  125. l min+incr    List incr+1 lines starting at min.
  126. l min-max    List lines.
  127. l line        List line;
  128. l        List next window.
  129. -        List previous window.
  130. w line        List window around line.
  131. l subname    List subroutine.
  132. f filename    Switch to filename.
  133. /pattern/    Search forwards for pattern; final / is optional.
  134. ?pattern?    Search backwards for pattern.
  135. L        List breakpoints and actions.
  136. S        List subroutine names.
  137. t        Toggle trace mode.
  138. b [line] [condition]
  139.         Set breakpoint; line defaults to the current execution line; 
  140.         condition breaks if it evaluates to true, defaults to \'1\'.
  141. b subname [condition]
  142.         Set breakpoint at first line of subroutine.
  143. d [line]    Delete breakpoint.
  144. D        Delete all breakpoints.
  145. a [line] command
  146.         Set an action to be done before the line is executed.
  147.         Sequence is: check for breakpoint, print line if necessary,
  148.         do action, prompt user if breakpoint or step, evaluate line.
  149. A        Delete all actions.
  150. V [pkg [vars]]    List some (default all) variables in package (default current).
  151. X [vars]    Same as \"V currentpackage [vars]\".
  152. < command    Define command before prompt.
  153. > command    Define command after prompt.
  154. ! number    Redo command (default previous command).
  155. ! -number    Redo number\'th to last command.
  156. H -number    Display last number commands (default all).
  157. q or ^D        Quit.
  158. p expr        Same as \"print DB::OUT expr\" in current package.
  159. = [alias value]    Define a command alias, or list current aliases.
  160. command        Execute as a perl statement in current package.
  161.  
  162. ";
  163.             next CMD; };
  164.         $cmd =~ /^t$/ && do {
  165.             $trace = !$trace;
  166.             print OUT "Trace = ".($trace?"on":"off")."\n";
  167.             next CMD; };
  168.         $cmd =~ /^S$/ && do {
  169.             foreach $subname (sort(keys %sub)) {
  170.             print OUT $subname,"\n";
  171.             }
  172.             next CMD; };
  173.         $cmd =~ s/^X\b/V $pkg/;
  174.         $cmd =~ /^V$/ && do {
  175.             $cmd = "V $pkg"; };
  176.         $cmd =~ /^V\b\s*(\S+)\s*(.*)/ && do {
  177.             local ($savout) = select(OUT);
  178.             $packname = $1;
  179.             @vars = split(' ',$2);
  180.             do 'dumpvar.pl' unless defined &main::dumpvar;
  181.             if (defined &main::dumpvar) {
  182.             &main::dumpvar($packname,@vars);
  183.             }
  184.             else {
  185.             print DB::OUT "dumpvar.pl not available.\n";
  186.             }
  187.             select ($savout);
  188.             next CMD; };
  189.         $cmd =~ /^f\b\s*(.*)/ && do {
  190.             $file = $1;
  191.             if (!$file) {
  192.             print OUT "The old f command is now the r command.\n";
  193.             print OUT "The new f command switches filenames.\n";
  194.             next CMD;
  195.             }
  196.             if (!defined $main::{'_<' . $file}) {
  197.             if (($try) = grep(m#^_<.*$file#, keys %main::)) {
  198.                 $file = substr($try,2);
  199.                 print "\n$file:\n";
  200.             }
  201.             }
  202.             if (!defined $main::{'_<' . $file}) {
  203.             print OUT "There's no code here anything matching $file.\n";
  204.             next CMD;
  205.             }
  206.             elsif ($file ne $filename) {
  207.             *dbline = "::_<$file";
  208.             $max = $#dbline;
  209.             $filename = $file;
  210.             $start = 1;
  211.             $cmd = "l";
  212.             } };
  213.         $cmd =~ /^l\b\s*([':A-Za-z_][':\w]*)/ && do {
  214.             $subname = $1;
  215.             $subname = "main::" . $subname unless $subname =~ /'|::/;
  216.             $subname = "main" . $subname if substr($subname,0,1)eq "'";
  217.             $subname = "main" . $subname if substr($subname,0,2)eq "::";
  218.             ($file,$subrange) = split(/:/,$sub{$subname});
  219.             if ($file ne $filename) {
  220.             *dbline = "::_<$file";
  221.             $max = $#dbline;
  222.             $filename = $file;
  223.             }
  224.             if ($subrange) {
  225.             if (eval($subrange) < -$window) {
  226.                 $subrange =~ s/-.*/+/;
  227.             }
  228.             $cmd = "l $subrange";
  229.             } else {
  230.             print OUT "Subroutine $1 not found.\n";
  231.             next CMD;
  232.             } };
  233.         $cmd =~ /^w\b\s*(\d*)$/ && do {
  234.             $incr = $window - 1;
  235.             $start = $1 if $1;
  236.             $start -= $preview;
  237.             $cmd = 'l ' . $start . '-' . ($start + $incr); };
  238.         $cmd =~ /^-$/ && do {
  239.             $incr = $window - 1;
  240.             $cmd = 'l ' . ($start-$window*2) . '+'; };
  241.         $cmd =~ /^l$/ && do {
  242.             $incr = $window - 1;
  243.             $cmd = 'l ' . $start . '-' . ($start + $incr); };
  244.         $cmd =~ /^l\b\s*(\d*)\+(\d*)$/ && do {
  245.             $start = $1 if $1;
  246.             $incr = $2;
  247.             $incr = $window - 1 unless $incr;
  248.             $cmd = 'l ' . $start . '-' . ($start + $incr); };
  249.         $cmd =~ /^l\b\s*(([\d\$\.]+)([-,]([\d\$\.]+))?)?/ && do {
  250.             $end = (!$2) ? $max : ($4 ? $4 : $2);
  251.             $end = $max if $end > $max;
  252.             $i = $2;
  253.             $i = $line if $i eq '.';
  254.             $i = 1 if $i < 1;
  255.             if ($emacs) {
  256.             print OUT "\032\032$filename:$i:0\n";
  257.             $i = $end;
  258.             } else {
  259.             for (; $i <= $end; $i++) {
  260.                 print OUT "$i:\t", $dbline[$i];
  261.                 last if $signal;
  262.             }
  263.             }
  264.             $start = $i;    # remember in case they want more
  265.             $start = $max if $start > $max;
  266.             next CMD; };
  267.         $cmd =~ /^D$/ && do {
  268.             print OUT "Deleting all breakpoints...\n";
  269.             for ($i = 1; $i <= $max ; $i++) {
  270.             if (defined $dbline{$i}) {
  271.                 $dbline{$i} =~ s/^[^\0]+//;
  272.                 if ($dbline{$i} =~ s/^\0?$//) {
  273.                 delete $dbline{$i};
  274.                 }
  275.             }
  276.             }
  277.             next CMD; };
  278.         $cmd =~ /^L$/ && do {
  279.             for ($i = 1; $i <= $max; $i++) {
  280.             if (defined $dbline{$i}) {
  281.                 print OUT "$i:\t", $dbline[$i];
  282.                 ($stop,$action) = split(/\0/, $dbline{$i});
  283.                 print OUT "  break if (", $stop, ")\n" 
  284.                 if $stop;
  285.                 print OUT "  action:  ", $action, "\n" 
  286.                 if $action;
  287.                 last if $signal;
  288.             }
  289.             }
  290.             next CMD; };
  291.         $cmd =~ /^b\b\s*([':A-Za-z_][':\w]*)\s*(.*)/ && do {
  292.             $subname = $1;
  293.             $cond = $2 || '1';
  294.             $subname = "${pkg}::" . $subname
  295.             unless $subname =~ /'|::/;
  296.             $subname = "main" . $subname if substr($subname,0,1) eq "'";
  297.             $subname = "main" . $subname if substr($subname,0,2) eq "::";
  298.             ($filename,$i) = split(/:/, $sub{$subname});
  299.             $i += 0;
  300.             if ($i) {
  301.             *dbline = "::_<$filename";
  302.             ++$i while $dbline[$i] == 0 && $i < $#dbline;
  303.             $dbline{$i} =~ s/^[^\0]*/$cond/;
  304.             } else {
  305.             print OUT "Subroutine $subname not found.\n";
  306.             }
  307.             next CMD; };
  308.         $cmd =~ /^b\b\s*(\d*)\s*(.*)/ && do {
  309.             $i = ($1?$1:$line);
  310.             $cond = $2 || '1';
  311.             if ($dbline[$i] == 0) {
  312.             print OUT "Line $i not breakable.\n";
  313.             } else {
  314.             $dbline{$i} =~ s/^[^\0]*/$cond/;
  315.             }
  316.             next CMD; };
  317.         $cmd =~ /^d\b\s*(\d+)?/ && do {
  318.             $i = ($1?$1:$line);
  319.             $dbline{$i} =~ s/^[^\0]*//;
  320.             delete $dbline{$i} if $dbline{$i} eq '';
  321.             next CMD; };
  322.         $cmd =~ /^A$/ && do {
  323.             for ($i = 1; $i <= $max ; $i++) {
  324.             if (defined $dbline{$i}) {
  325.                 $dbline{$i} =~ s/\0[^\0]*//;
  326.                 delete $dbline{$i} if $dbline{$i} eq '';
  327.             }
  328.             }
  329.             next CMD; };
  330.         $cmd =~ /^<\s*(.*)/ && do {
  331.             $pre = action($1);
  332.             next CMD; };
  333.         $cmd =~ /^>\s*(.*)/ && do {
  334.             $post = action($1);
  335.             next CMD; };
  336.         $cmd =~ /^a\b\s*(\d+)(\s+(.*))?/ && do {
  337.             $i = $1;
  338.             if ($dbline[$i] == 0) {
  339.             print OUT "Line $i may not have an action.\n";
  340.             } else {
  341.             $dbline{$i} =~ s/\0[^\0]*//;
  342.             $dbline{$i} .= "\0" . action($3);
  343.             }
  344.             next CMD; };
  345.         $cmd =~ /^n$/ && do {
  346.             $single = 2;
  347.             $laststep = $cmd;
  348.             last CMD; };
  349.         $cmd =~ /^s$/ && do {
  350.             $single = 1;
  351.             $laststep = $cmd;
  352.             last CMD; };
  353.         $cmd =~ /^c\b\s*(\d*)\s*$/ && do {
  354.             $i = $1;
  355.             if ($i) {
  356.             if ($dbline[$i] == 0) {
  357.                 print OUT "Line $i not breakable.\n";
  358.                 next CMD;
  359.             }
  360.             $dbline{$i} =~ s/(\0|$)/;9$1/;    # add one-time-only b.p.
  361.             }
  362.             for ($i=0; $i <= $#stack; ) {
  363.             $stack[$i++] &= ~1;
  364.             }
  365.             last CMD; };
  366.         $cmd =~ /^r$/ && do {
  367.             $stack[$#stack] |= 2;
  368.             last CMD; };
  369.         $cmd =~ /^T$/ && do {
  370.             local($p,$f,$l,$s,$h,$a,@a,@sub);
  371.             for ($i = 1; ($p,$f,$l,$s,$h,$w) = caller($i); $i++) {
  372.             @a = ();
  373.             for $arg (@args) {
  374.                 $_ = "$arg";
  375.                 s/'/\\'/g;
  376.                 s/([^\0]*)/'$1'/
  377.                 unless /^(?: -?[\d.]+ | \*[\w:]* )$/x;
  378.                 s/([\200-\377])/sprintf("M-%c",ord($1)&0177)/eg;
  379.                 s/([\0-\37\177])/sprintf("^%c",ord($1)^64)/eg;
  380.                 push(@a, $_);
  381.             }
  382.             $w = $w ? '@ = ' : '$ = ';
  383.             $a = $h ? '(' . join(', ', @a) . ')' : '';
  384.             push(@sub, "$w$s$a from file $f line $l\n");
  385.             last if $signal;
  386.             }
  387.             for ($i=0; $i <= $#sub; $i++) {
  388.             last if $signal;
  389.             print OUT $sub[$i];
  390.             }
  391.             next CMD; };
  392.         $cmd =~ /^\/(.*)$/ && do {
  393.             $inpat = $1;
  394.             $inpat =~ s:([^\\])/$:$1:;
  395.             if ($inpat ne "") {
  396.             eval '$inpat =~ m'."\a$inpat\a";    
  397.             if ($@ ne "") {
  398.                 print OUT "$@";
  399.                 next CMD;
  400.             }
  401.             $pat = $inpat;
  402.             }
  403.             $end = $start;
  404.             eval '
  405.             for (;;) {
  406.             ++$start;
  407.             $start = 1 if ($start > $max);
  408.             last if ($start == $end);
  409.             if ($dbline[$start] =~ m'."\a$pat\a".'i) {
  410.                 if ($emacs) {
  411.                 print OUT "\032\032$filename:$start:0\n";
  412.                 } else {
  413.                 print OUT "$start:\t", $dbline[$start], "\n";
  414.                 }
  415.                 last;
  416.             }
  417.             } ';
  418.             print OUT "/$pat/: not found\n" if ($start == $end);
  419.             next CMD; };
  420.         $cmd =~ /^\?(.*)$/ && do {
  421.             $inpat = $1;
  422.             $inpat =~ s:([^\\])\?$:$1:;
  423.             if ($inpat ne "") {
  424.             eval '$inpat =~ m'."\a$inpat\a";    
  425.             if ($@ ne "") {
  426.                 print OUT "$@";
  427.                 next CMD;
  428.             }
  429.             $pat = $inpat;
  430.             }
  431.             $end = $start;
  432.             eval '
  433.             for (;;) {
  434.             --$start;
  435.             $start = $max if ($start <= 0);
  436.             last if ($start == $end);
  437.             if ($dbline[$start] =~ m'."\a$pat\a".'i) {
  438.                 if ($emacs) {
  439.                 print OUT "\032\032$filename:$start:0\n";
  440.                 } else {
  441.                 print OUT "$start:\t", $dbline[$start], "\n";
  442.                 }
  443.                 last;
  444.             }
  445.             } ';
  446.             print OUT "?$pat?: not found\n" if ($start == $end);
  447.             next CMD; };
  448.         $cmd =~ /^!+\s*(-)?(\d+)?$/ && do {
  449.             pop(@hist) if length($cmd) > 1;
  450.             $i = ($1?($#hist-($2?$2:1)):($2?$2:$#hist));
  451.             $cmd = $hist[$i] . "\n";
  452.             print OUT $cmd;
  453.             redo CMD; };
  454.         $cmd =~ /^!(.+)$/ && do {
  455.             $pat = "^$1";
  456.             pop(@hist) if length($cmd) > 1;
  457.             for ($i = $#hist; $i; --$i) {
  458.             last if $hist[$i] =~ $pat;
  459.             }
  460.             if (!$i) {
  461.             print OUT "No such command!\n\n";
  462.             next CMD;
  463.             }
  464.             $cmd = $hist[$i] . "\n";
  465.             print OUT $cmd;
  466.             redo CMD; };
  467.         $cmd =~ /^H\b\s*(-(\d+))?/ && do {
  468.             $end = $2?($#hist-$2):0;
  469.             $hist = 0 if $hist < 0;
  470.             for ($i=$#hist; $i>$end; $i--) {
  471.             print OUT "$i: ",$hist[$i],"\n"
  472.                 unless $hist[$i] =~ /^.?$/;
  473.             };
  474.             next CMD; };
  475.         $cmd =~ s/^p( .*)?$/print DB::OUT$1/;
  476.         $cmd =~ /^=/ && do {
  477.             if (local($k,$v) = ($cmd =~ /^=\s*(\S+)\s+(.*)/)) {
  478.             $alias{$k}="s~$k~$v~";
  479.             print OUT "$k = $v\n";
  480.             } elsif ($cmd =~ /^=\s*$/) {
  481.             foreach $k (sort keys(%alias)) {
  482.                 if (($v = $alias{$k}) =~ s~^s\~$k\~(.*)\~$~$1~) {
  483.                 print OUT "$k = $v\n";
  484.                 } else {
  485.                 print OUT "$k\t$alias{$k}\n";
  486.                 };
  487.             };
  488.             };
  489.             next CMD; };
  490.         }
  491.         $evalarg = $cmd; &eval;
  492.         print OUT "\n";
  493.     }
  494.     if ($post) {
  495.         $evalarg = $post; &eval;
  496.     }
  497.     }
  498.     ($@, $!, $,, $/, $\, $^W) = @saved;
  499.     ();
  500. }
  501.  
  502. sub save {
  503.     @saved = ($@, $!, $,, $/, $\, $^W);
  504.     $, = ""; $/ = "\n"; $\ = ""; $^W = 0;
  505. }
  506.  
  507. # The following takes its argument via $evalarg to preserve current @_
  508.  
  509. sub eval {
  510.     eval "$usercontext $evalarg; &DB::save";
  511.     print OUT $@;
  512. }
  513.  
  514. sub action {
  515.     local($action) = @_;
  516.     while ($action =~ s/\\$//) {
  517.     print OUT "+ ";
  518.     $action .= &gets;
  519.     }
  520.     $action;
  521. }
  522.  
  523. sub gets {
  524.     local($.);
  525.     <IN>;
  526. }
  527.  
  528. sub catch {
  529.     $signal = 1;
  530. }
  531.  
  532. sub sub {
  533.     push(@stack, $single);
  534.     $single &= 1;
  535.     $single |= 4 if $#stack == $deep;
  536.     if (wantarray) {
  537.     @i = &$sub;
  538.     $single |= pop(@stack);
  539.     @i;
  540.     }
  541.     else {
  542.     $i = &$sub;
  543.     $single |= pop(@stack);
  544.     $i;
  545.     }
  546. }
  547.  
  548. $trace = $signal = $single = 0;    # uninitialized warning suppression
  549.  
  550. @hist = ('?');
  551. $SIG{'INT'} = "DB::catch";
  552. $deep = 100;        # warning if stack gets this deep
  553. $window = 10;
  554. $preview = 3;
  555.  
  556. @stack = (0);
  557. @ARGS = @ARGV;
  558. for (@args) {
  559.     s/'/\\'/g;
  560.     s/(.*)/'$1'/ unless /^-?[\d.]+$/;
  561. }
  562.  
  563. if (-f $rcfile) {
  564.     do "./$rcfile";
  565. }
  566. elsif (-f "$ENV{'LOGDIR'}/$rcfile") {
  567.     do "$ENV{'LOGDIR'}/$rcfile";
  568. }
  569. elsif (-f "$ENV{'HOME'}/$rcfile") {
  570.     do "$ENV{'HOME'}/$rcfile";
  571. }
  572.  
  573. 1;
  574.